package com.geekaca.web.servlet;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.interfaces.Claim;
import com.geekaca.web.utils.JwtUtil;
import com.geekaca.web.utils.Result;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

//@WebFilter(urlPatterns = {"/brands", "/updateBrand", "/updateBrand.jsp", "/addBrand", "/addBrand.jsp"})
//@WebFilter(urlPatterns = {"/updateBrand", "/addBrand", "/delete"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
//        servletResponse.setCharacterEncoding("UTF-8");
//        resp.setHeader("Content-type", "text/html; charset=utf-8");


        System.out.println("into filter...");
        String token = req.getHeader("token");
        System.out.println("token: " + token);

        PrintWriter writer = resp.getWriter();
        Result result = new Result();
        //判断token的有效性
        if (token == null || "null".equals(token)) {
            //说明没有登录
            result.setCode(403);
            result.setMsg("非法请求，请先登录, 即将返回登录。。。");
            String s = JSON.toJSONString(result);
            writer.write(s);
            writer.flush();
            return;
        }

        try {
            Map<String, Claim> stringClaimMap = JwtUtil.verifyToken(token);
//            System.out.println(stringClaimMap);
//            Claim id_ = stringClaimMap.get("id");
//            Integer id = Integer.valueOf(id_.asInt());


            if (stringClaimMap == null) {
                result.setCode(403);
                result.setMsg("非法请求，请先登录");
                String s = JSON.toJSONString(result);
                writer.write(s);
                writer.flush();
            } else {
                // 放行请求
                filterChain.doFilter(servletRequest, servletResponse);
                System.out.println(" 从资源返回给前端之前......");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }


//        if (token == null) {
//            System.out.println("token is null...");
//            resp.setHeader("Content-type", "text/html; charset=utf-8");
//            PrintWriter writer = resp.getWriter();
//            writer.write("<h1>非法请求，请先登录, 即将返回登录。。。</h1>");
////            try {
////
////                Thread.sleep(2000);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//            return;
////
//
//        } else {
//            // 放行请求
//            filterChain.doFilter(servletRequest, servletResponse);
//            System.out.println(" 从资源返回给前端之前......");
//        }


    }
}
